
import re


regex = r"_([a-zA-Z_0-9]+)\s*="

def help_txt():
    return """
    参数配置管理自动化使用样例, 第1行 参数类名 第2行 获取管理实例的函数名 第3-End 参数项
AxisLimitConfig
axis_limits_config_instance
self._x_limit_neg = 0
        self._x_limit_pos = 0
        self._y_limit_neg = 0
        self._y_limit_pos = 0
        self._z_limit_neg = 0
        self._z_limit_pos = 0
        self._a_limit_neg = 0
        self._a_limit_pos = 0
        self._b_limit_neg = 0
        self._b_limit_pos = 0
"""

def process_txt(txt):
    if not txt:
        return "输入为空"
    txt = txt.replace("\r", "\n").replace("\r\n", "\n")
    return_result = []
    
    # 读取参数
    strs = txt.split("\n")
    ui_class_name = strs[0]
    config_instance_func_name = strs[1]
    params = "".join(strs[2:])
    
    matches = re.finditer(regex, params)    
    params_list = []
    for match in matches:
        params_list.append(match.group(1))
    
    return_result.append("\r")
    return_result.append("----------生成init_with_json函数中的内容--------------------\n")
    # 生成init_with_json函数中的内容
    temp_1 = "self.{} = data[\"{}\"]"
    results_1 = []
    for p in params_list:
        results_1.append(temp_1.format("_" + p, p))
    str_1 = "\r".join(results_1)
    return_result.append(str_1)
    
    return_result.append("\r")
    
    return_result.append("----------生成build_data_dict函数中的内容--------------------\n")
    # 生成build_data_dict函数中的内容
    temp_2 = "    \"{}\": self.{},"
    results_2 = []
    for p in params_list:
        results_2.append(temp_2.format(p, "_" + p))
    str_2 = "\r".join(results_2)
    return_result.append("{\r" + str_2 + "\r}\r")
    
    return_result.append("\r")
    
    return_result.append("----------生成init_ui函数的内容--------------------\n")
    # 生成init_ui函数的内容
    head_str = "from comm_modules.parameter.api import {}\r\r_C = {}()".format(config_instance_func_name, config_instance_func_name)
    temp_3 = "{} = _C.read_data(\"{}\")"
    temp_3_ui = "self.ui.lineEdit_{}.setText(\"%f\" % {})"
    
    results_3 = []
    for p in params_list:
        results_3.append(temp_3.format("_" + p, p))
    results_3.append("\r")
    
    for p in params_list:
        results_3.append(temp_3_ui.format(p, "_" + p))
        
    str_3 = "\r".join(results_3)
    return_result.append(head_str + "\r" + str_3)
    
    return_result.append("\r")
    return_result.append("----------生成apply的内容--------------------\n")
    
    # 生成apply的内容
    temp_4 = "    _{} = float(self.ui.lineEdit_{}.text())"
    temp_4_2 = "_C.write_data(\"{}\", _{})"
    results_4 = ["try:"]
    for p in params_list:
        results_4.append(temp_4.format(p, p)) 
    results_4.append("except Exception as e:")
    results_4.append("    _err_format = _translate(\"{}\", \"please input valid double value!!!\")".format(ui_class_name))
    results_4.append("    _ERROR(_err_format)")
    results_4.append("    return")
    
    results_4.append("_C = {}()".format(config_instance_func_name))
    for p in params_list:
        results_4.append(temp_4_2.format(p, p))
        
    results_4.append("_C.apply_config()")
    
    str_4 = "\r".join(results_4)
    return_result.append(str_4)
    
    return_result.append("\r")
    return_result.append("-------------生成config_ui函数的内容-----------\n")
    # 生成config_ui函数的内容
    results_5 = ["rx = QtCore.QRegExp(\"[0-9\.\-]+$\")", "validator = QtGui.QRegExpValidator(rx, self)"]
    temp_5 = "self.ui.lineEdit_{}.setValidator(validator)"
    
    for p in params_list:
        results_5.append(temp_5.format(p))
    
    str_5 = "\r".join(results_5)
    return_result.append(str_5)
    
    return "".join(return_result)
